import java.util.*;

public class Tree {

    static class TreeNode {
        private char val;
        public TreeNode right;
        public TreeNode left;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode node() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');

        A.right = C;
        A.left = B;
        B.right = E;
        B.left = D;
        C.right = G;
        C.left = F;


        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //非递归前序遍历
    public List<Character> preorderTraversal1(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;


        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                list.add(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }

        return list;
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);

        System.out.print(root.val + " ");
        inOrder(root.right);

    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);

        System.out.print(root.val + " ");
    }
    //后序遍历非递归
    public List<Integer> postorderTraversal(TreeNode root) {
    }

    public List<TreeNode> preOrder1(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder1(root.left);

        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder1(root.right);
        ret.addAll(rightTree);
        return ret;
    }
   /* List<towTree> ret = new ArrayList<>();
    public List<towTree> preOrder1(towTree root){

        if(root == null){
            return ret;
        }
        ret.add(root);
         preOrder1(root.right);
         preOrder1(root.left);
        return ret;
    }*/

    //求二叉树的高度/深度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);

        return right > left ? right + 1 : left + 1;
    }

    // 获取树中节点的个数
    int size1 = 0;

    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        size1++;
        size(root.left);
        size(root.right);
        return size1;
    }

    // 获取叶子节点的个数
    int NodeCount = 0;

    public void getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.right == null && root.left == null) {
            NodeCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);


    }

    // 子问题思路-求叶子结点个数
    public int getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount1(root.left) +
                getLeafNodeCount1(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    // 检测值为value的元素是否存在
    public boolean find(TreeNode root, int val) {
        if (root == null) {
            return false;
        } else if (root.val == val) {

            return true;
        }


        return find(root.left, val) ||
                find(root.right, val);


    }

    //判断q二叉树和p二叉树是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p != null && q == null || q != null && p == null) {
            return false;
        }
        if (p == null && q == null) {
            return true;

        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //平衡二叉树
    public boolean isBalanced(TreeNode root) {
        return true;
    }

    //判断是否对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetric1(root.right, root.right) &&
                isSymmetric1(root.right, root.right);

    }

    public boolean isSymmetric1(TreeNode p, TreeNode q) {
        if (p == null && q != null || q == null && p != null) {
            return false;
        }
        if (p == null & q == null) {
            return true;
        }
        if (p.val == q.val) {
            return true;
        }
        return isSymmetric1(p.left, q.right);

    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }

        }
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> tmp = new LinkedList<>();
        tmp.offer(root);
        while (!tmp.isEmpty()) {
            //只要节点不为空，就把子节点全部放入队列当中
            TreeNode cur = tmp.poll();
            if (cur != null) {
                tmp.offer(cur.left);
                tmp.offer(cur.right);
            } else {
                break;
            }
        }
        while (!tmp.isEmpty()) {
            TreeNode cur = tmp.peek();
            if (cur != null) {
                return false;
            } else {
                tmp.poll();
            }
        }
        return true;
    }

    //最近公共祖先的定义为：“对于有根树 T 的两个节点 p、q，
    // 最近公共祖先表示为一个节点 x，满足 x 是 p、q 的祖先且 x 的深度尽可能大（一个节点也可以是它自己的祖先）
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root == p || root == q) {
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
        TreeNode rightTree = lowestCommonAncestor(root.right, p, q);
        if (leftTree == null && rightTree == null) {
            return null;
        } else if (leftTree != null) {
            return leftTree;
        } else {
            return rightTree;
        }
    }

    //给你二叉树的根节点 root ，请你采用前序遍历的方式，
    // 将二叉树转化为一个由括号和整数组成的字符串，返回构造出的字符串。
    //空节点使用一对空括号对 "()" 表示，
    // 转化后需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder = new StringBuilder();
        tree2str1(root,stringBuilder);
        return stringBuilder.toString();
    }
    public void tree2str1(TreeNode x,StringBuilder stringBuilder){
             if(x == null){
                 return ;
             }
             stringBuilder.append(x.val);
             if(x.left!=null){
                 stringBuilder.append("(");
                 tree2str1(x.left,stringBuilder);
                 stringBuilder.append(")");
             }else {
                 if(x.right == null){
                     return ;
                 }else{
                     stringBuilder.append("()");
                 }
             }
        if(x.right!=null){
            stringBuilder.append("(");
            tree2str1(x.right,stringBuilder);
            stringBuilder.append(")");
        }else {
          return;
        }


    }
    //已知前序和后序 构造二叉树
    //前序的下标

    public TreeNode buildTree(int[] postorder, int[] inorder) {
        return buildTreechild(inorder, postorder, 0, inorder.length - 1);
    }
    int  posIndex = postorder.length-1;
    public TreeNode buildTreechild (int[] inorder, int[] postorder,int inbegin,int inend){
        if(inbegin>inend){
            return null;
        }
        TreeNode root = new TreeNode(postorder[posIndex]);
        TreeNode inIndex = findIndex(inorder,inbegin,inend,root);
        if(findIndex()==-1){
            return
        }
        root++;
        root.left = buildTreechild(postorder,inorder,inbegin,inIndex-1);
        root.right = buildTreechild(postorder,inorder,inIndex+1,inend);
        return root;

    }
    public int findIndex(int[] inorder,int inbegin,int inend,int key){
        for(int i =inbegin;i<=inend;i++){
            if(inorder[i] == key){
                return i;
            }
        }
        return -1;
    }





}








